﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Data;

/// <summary>
/// Summary description for PaymentReceiptPDFOutput
/// </summary>
public class PaymentReceiptPDFOutput
{
    private string _carplatenumber;
    public string CarPlate
    {
        get { return _carplatenumber; }
        set { _carplatenumber = value; }
    }

    private string _customername;
    public string CustomerName
    {
        get { return _customername; }
        set { _customername = value; }
    }

    private string _quoterefno;
    public string QuoteRefNo
    {
        get { return _quoterefno; }
        set { _quoterefno = value; }
    }

    private string _address1;
    public string Address1
    {
        get { return _address1; }
        set { _address1 = value; }
    }

    private string _address2;
    public string Address2
    {
        get { return _address2; }
        set { _address2 = value; }
    }

    private string _address3;
    public string Address3
    {
        get { return _address3; }
        set { _address3 = value; }
    }

    private string _typeofpolicy;
    public string TypeOfPolicy
    {
        get { return _typeofpolicy; }
        set { _typeofpolicy = value; }
    }

    private string _makemodel;
    public string MakeModel
    {
        get { return _makemodel; }
        set { _makemodel = value; }
    }

    private string _annualpremium;
    public string AnnualPremium
    {
        get { return _annualpremium; }
        set { _annualpremium = value; }
    }

    private bool _isbanktransfer;
    public bool IsBankTransfer
    {
        get { return _isbanktransfer; }
        set { _isbanktransfer = value; }
    }

    private bool _iscash;
    public bool IsCash
    {
        get { return _iscash; }
        set { _iscash = value; }
    }

    private bool _ischeckno;
    public bool IsCheckNo
    {
        get { return _ischeckno; }
        set { _ischeckno = value; }
    }

    private string _checkno;
    public string CheckNo
    {
        get { return _checkno; }
        set { _checkno = value; }
    }

    private bool _iscreditcard;
    public bool IsCreditCard
    {
        get { return _iscreditcard; }
        set { _iscreditcard = value; }
    }

    private bool _isvisa;
    public bool IsVisa
    {
        get { return _isvisa; }
        set { _isvisa = value; }
    }

    private bool _ismaster;
    public bool IsMaster
    {
        get { return _ismaster; }
        set { _ismaster = value; }
    }

    public PaymentReceiptPDFOutput()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public string GeneratePDF()
    {
        // Create a new PDF document
        PdfDocument document = new PdfDocument();
        // Set font encoding to unicode

        XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

        document.Info.Title = "Created with PDFsharp";

        // Create an empty page
        PdfPage page = document.AddPage();

        // Get an XGraphics object for drawing
        XGraphics gfx = XGraphics.FromPdfPage(page);

        //XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

        // Create a font
        XFont fontHeader = new XFont("Tahoma", 13, XFontStyle.Bold, options);
        XFont font = new XFont("Tahoma", 10d, XFontStyle.Regular, options);
        XFont fontFooter = new XFont("Tahoma", 11, XFontStyle.Bold, options);
        XFont fontAddress = new XFont("Tahoma", 12, XFontStyle.Regular, options);
        XColor _xcolor = new XColor();
        _xcolor.R = 0;
        _xcolor.G = 0;
        _xcolor.B = 0;
        System.Drawing.Rectangle _rect = new System.Drawing.Rectangle(40, 40, 535, 435);
        XPen _pen = new XPen(_xcolor);
        // Draw the text
        //gfx.DrawRectangle(_pen, _rect);
        //gfx.DrawString("Hello, World!", font, XBrushes.Black,
        //  new XRect(15, 15, 15, 15),
        //  XStringFormats.TopLeft);

        gfx.DrawRectangle(_pen, _rect);

        int curX = 60;
        int curY = 60;

        //gfx.DrawImage(XImage.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/logos/gluaygluay.png"), new XRect(curX + 400, 50, 39, 27));
        gfx.DrawImage(XImage.FromFile(AppDomain.CurrentDomain.BaseDirectory + "/images/public/logo/gluaygluay.png"), new XRect(curX, curY, 83, 73));
        //curY += 20;

        //curY += 15;
        //gfx.DrawString("CompareXpress Non-Life Insurance Broker (Thailand) Ltd.", fontFooter, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        //gfx.DrawString("บริษัทคอมแพร์เอ็กซ์เพรส นายหน้าประกันวินาศภัย (ประเทศไทย) จำกัด", fontHeader, XBrushes.Black, new XRect(curX + 90, curY, 50, 20), XStringFormat.TopLeft);
        gfx.DrawString("บริษัท คอมแพร์เอ็กซ์เพรส นายหน้าประกันภัย (ประเทศไทย) จำกัด", fontHeader, XBrushes.Black, new XRect(curX + 90, curY, 50, 20), XStringFormat.TopLeft);
        curY += 15;

        //gfx.DrawString("2 K.C.C. Building 2nd floor", fontFooter, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        gfx.DrawString("เลขที่นายหน้าประกันวินาศภัยนิตติบุคคลเลขที่: ว00016/2553", fontAddress, XBrushes.Black, new XRect(curX + 90, curY, 50, 20), XStringFormat.TopLeft);
        curY += 15;

        //gfx.DrawString("Silom Soi 9 Silom Road Bangrak", fontFooter, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        gfx.DrawString("2 อาคาร เค.ซี.ซี. ชั้น 2 สีลมซอย 9 ถนนสีลม เขตบางรัก กรุงเทพฯ 10500", fontAddress, XBrushes.Black, new XRect(curX + 90, curY, 50, 20), XStringFormat.TopLeft);
        curY += 15;

        //gfx.DrawString("Bangkok 10500", fontFooter, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        gfx.DrawString("โทร 02-268-8222  โทรสาร 02-266-6339", fontAddress, XBrushes.Black, new XRect(curX + 90, curY, 50, 20), XStringFormat.TopLeft);
        curY += 15;

        gfx.DrawString("อีเมล์: support@gluaygluay.com", fontAddress, XBrushes.Blue, new XRect(curX + 90, curY, 50, 20), XStringFormat.TopLeft);
        curY += 30;

        
        gfx.DrawString("เอกสารยืนยันการชำระค่าเบี้ยประกันภัย", fontHeader, XBrushes.Blue, new XRect(curX, curY, 500, 20), XStringFormat.Center);

        curY += 30;
        //ได้รับค่าเบี้ยประกันภัย จาก   //Premium received from

        gfx.DrawString(Resources.CarInsuranceResultPage.DateNew, font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(DateTime.Now.ToString("dd-MMM-yyyy"), font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 15;
        gfx.DrawString(Resources.CarInsurance.ReferenceNo, font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_quoterefno, font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        curY += 15;

        curX += 50;
        gfx.DrawString("ได้รับค่าเบี้ยประกันภัย จาก", font, XBrushes.Blue, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        
        curY += 15;
        gfx.DrawString(Resources.CarInsuranceResultPage.CustomerName, font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_customername, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        curY += 15;
        gfx.DrawString(Resources.General.CustomerAddress, font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_address1, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);
        curY += 15;
        gfx.DrawString(_address2, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);
        curY += 15;
        gfx.DrawString(_address3, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        curY += 20;
        gfx.DrawString(Resources.CarInsurance.InsuranceType, font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_typeofpolicy, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        curY += 15;
        gfx.DrawString(Resources.CarInsurance.CarMakeModel, font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 100, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_makemodel, font, XBrushes.Black, new XRect(curX + 105, curY, 50, 15), XStringFormat.TopLeft);

        curY += 30;
        gfx.DrawString(Resources.ProposalForm.AnnualPremium, font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 120, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString(_annualpremium, font, XBrushes.Black, new XRect(curX + 125, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString("บาท", font, XBrushes.Black, new XRect(curX + 170, curY, 50, 15), XStringFormat.TopLeft);
        gfx.DrawString("(รวมอากรแสตมป์และภาษีมูลค่าเพิ่ม)", font, XBrushes.Black, new XRect(curX + 195, curY, 50, 15), XStringFormat.TopLeft);

        curY += 30;
        gfx.DrawString("ชำระโดย", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);

        gfx.DrawString(Resources.General.BankTransfer, font, XBrushes.Black, new XRect(curX + 50, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 120, curY, 50, 15), XStringFormat.TopLeft);
        if (_isbanktransfer)
        {
            gfx.DrawString("√", font, XBrushes.Black, new XRect(curX + 125, curY, 50, 15), XStringFormat.TopLeft);
        }
        else
        {
            gfx.DrawString("X", font, XBrushes.Black, new XRect(curX + 125, curY, 50, 15), XStringFormat.TopLeft);
        }

        gfx.DrawString(Resources.General.Cash, font, XBrushes.Black, new XRect(curX + 150, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 180, curY, 50, 15), XStringFormat.TopLeft);

        if (_iscash)
        {
            gfx.DrawString("√", font, XBrushes.Black, new XRect(curX + 185, curY, 50, 15), XStringFormat.TopLeft);
        }
        else
        {
            gfx.DrawString("X", font, XBrushes.Black, new XRect(curX + 185, curY, 50, 15), XStringFormat.TopLeft);
        }

        gfx.DrawString(Resources.General.CheckNo, font, XBrushes.Black, new XRect(curX + 220, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 260, curY, 50, 15), XStringFormat.TopLeft);

        if (_ischeckno)
        {
            gfx.DrawString("√", font, XBrushes.Black, new XRect(curX + 265, curY, 50, 15), XStringFormat.TopLeft);
            gfx.DrawString(_checkno, font, XBrushes.Black, new XRect(curX + 295, curY, 50, 15), XStringFormat.TopLeft);
        }
        else
        {
            gfx.DrawString("X", font, XBrushes.Black, new XRect(curX + 265, curY, 50, 15), XStringFormat.TopLeft);
        }

        curY += 15;
        gfx.DrawString(Resources.General.CreditCard, font, XBrushes.Black, new XRect(curX + 50, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 120, curY, 50, 15), XStringFormat.TopLeft);
        if (_iscreditcard)
        {
            gfx.DrawString("√", font, XBrushes.Black, new XRect(curX + 125, curY, 50, 15), XStringFormat.TopLeft);
        }
        else
        {
            gfx.DrawString("X", font, XBrushes.Black, new XRect(curX + 125, curY, 50, 15), XStringFormat.TopLeft);
        }

        gfx.DrawString(Resources.General.Visa, font, XBrushes.Black, new XRect(curX + 150, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 180, curY, 50, 15), XStringFormat.TopLeft);

        if (_isvisa)
        {
            gfx.DrawString("√", font, XBrushes.Black, new XRect(curX + 185, curY, 50, 15), XStringFormat.TopLeft);
        }
        else
        {
            gfx.DrawString("X", font, XBrushes.Black, new XRect(curX + 185, curY, 50, 15), XStringFormat.TopLeft);
        }

        gfx.DrawString(Resources.General.Master, font, XBrushes.Black, new XRect(curX + 220, curY, 30, 15), XStringFormat.TopLeft);
        gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 260, curY, 50, 15), XStringFormat.TopLeft);

        if (_ismaster)
        {
            gfx.DrawString("√", font, XBrushes.Black, new XRect(curX + 265, curY, 50, 15), XStringFormat.TopLeft);
        }
        else
        {
            gfx.DrawString("X", font, XBrushes.Black, new XRect(curX + 265, curY, 50, 15), XStringFormat.TopLeft);
        }

        curX -= 50;
        curY += 30;
        gfx.DrawString("หมายเหตุ", fontHeader, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);

        curY += 15;
        gfx.DrawString("- เอกสารฉบับนี้จัดทำด้วยระบบคอมพิวเตอร์ ถือว่าสมบูรณ์โดยไม่ต้องมีลายมือชื่อผู้รับมอบอำนาจ", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);

        curY += 15;
        gfx.DrawString("- ใบเสร็จรับเงินตัวจริงจะทำการออกให้ท่านโดยบริษัทประกันฯ และส่งให้ท่านพร้อมกับกรมธรรม์ต่อไป", font, XBrushes.Black, new XRect(curX, curY, 30, 15), XStringFormat.TopLeft);

        string _filePath = GetFilePath();
        string filename = _filePath + "PaymentReceipt_" + _quoterefno + "_" + DateTime.Now.ToString("_ddMMMyyyy_hhmmss_tt") + ".pdf";
        //document.Save(filename);
        // ...and start a viewer.
        //Response.Redirect(filename);
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        document.Save(filename);
        stream.Close();
        document.Close();
        return filename;
    }

    private string GetFilePath()
    {
        string _filePath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TH.NonGGFileOutputPath").ToString();
        _filePath += "pdfoutput\\carinsurance\\";
        CE.InputOut.MyIO _myIO = new CE.InputOut.MyIO();

        string _curYear = DateTime.Now.ToString("yyyy");
        if (!_myIO.CheckDirectory(_filePath + _curYear))
        {
            if (_myIO.CreateDirectory(_filePath, _curYear))
            {
                _filePath += _curYear + "\\";
            }
        }
        else
        {
            _filePath += _curYear + "\\";
        }

        string _curMonth = DateTime.Now.ToString("MMMM");
        if (!_myIO.CheckDirectory(_filePath + _curMonth))
        {
            if (_myIO.CreateDirectory(_filePath, _curMonth))
            {
                _filePath += _curMonth + "\\";
            }
        }
        else
        {
            _filePath += _curMonth + "\\";
        }

        string _todaydate = DateTime.Now.ToString("ddMMMMyyyy");
        if (!_myIO.CheckDirectory(_filePath + _todaydate))
        {
            if (_myIO.CreateDirectory(_filePath, _todaydate))
            {
                _filePath += _todaydate + "\\";
            }
        }
        else
        {
            _filePath += _todaydate + "\\";
        }

        string _payments = "Payments_" + _todaydate;
        if (!_myIO.CheckDirectory(_filePath + _payments))
        {
            if (_myIO.CreateDirectory(_filePath, _payments))
            {
                _filePath += _payments + "\\";
            }
        }
        else
        {
            _filePath += _payments + "\\";
        }

        return _filePath;
    }
}
